package com.fantasy.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;
@MultipartConfig(location = "e:/Video/",fileSizeThreshold = 1024*1024,maxFileSize = 1024*1024*5,maxRequestSize = 1024*1024*5*5)
@WebServlet("/admin/videoLoad.do")
public class FileLoadServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charSet=utf-8");
        PrintWriter writer = resp.getWriter();
        Part part = req.getPart("img");
        //获取文件后缀名，也就是文件类型
        String realFileName = part.getSubmittedFileName();
        String fileType = realFileName.substring(realFileName.lastIndexOf("."));
        //通过UUID生成随机文件名0
        String fileName= UUID.randomUUID().toString()+ fileType;
        //获取项目下该文件夹路径
        String path="E:/Video/"+fileName;
        part.write(path);
        //将文件名回传给前端
        writer.write(fileName);
    }

}
